//
//  TJRoundingCornerImageView.m
//
//  Created by xtjun on 2019/11/23.
//  Copyright © 2019 xiayonghong. All rights reserved.
//

#import "TJRoundingCornerImageView.h"

@implementation TJRoundingCornerImageView {
    CAShapeLayer *_shapeLayer;
}


- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        _shapeLayer = [CAShapeLayer layer];
    }
    return self;
}

- (void)setRadius:(CGFloat)radius {
    _radius = radius;
    [self setNeedsLayout];
}

- (void)setCorners:(UIRectCorner)corners {
    _corners = corners;
    [self setNeedsLayout];
}

+ (instancetype)imageRoundingCorners:(UIRectCorner)corners radius:(CGFloat)radius {
    TJRoundingCornerImageView *imageView = [[self alloc] initWithFrame:CGRectZero];
    imageView -> _radius = radius;
    imageView -> _corners = corners;
    return imageView;
    
}

- (void)layoutSubviews {
    [super layoutSubviews];

    _shapeLayer.path = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners: _corners cornerRadii:CGSizeMake(_radius, _radius)].CGPath;
    self.layer.mask = _shapeLayer;
    
}

@end


@implementation TJRoundingCornerView {
    CAShapeLayer *_shapeLayer;
}


- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        _shapeLayer = [CAShapeLayer layer];
    }
    return self;
}

- (void)setRadius:(CGFloat)radius {
    _radius = radius;
    [self setNeedsLayout];
}

- (void)setCorners:(UIRectCorner)corners {
    _corners = corners;
    [self setNeedsLayout];
}

+ (instancetype)roundingCorners:(UIRectCorner)corners radius:(CGFloat)radius {
    TJRoundingCornerView *view = [[self alloc] initWithFrame:CGRectZero];
    view -> _radius = radius;
    view -> _corners = corners;
    return view;
    
}

- (void)layoutSubviews {
    [super layoutSubviews];

    _shapeLayer.path = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners: _corners cornerRadii:CGSizeMake(_radius, _radius)].CGPath;
    self.layer.mask = _shapeLayer;
    
}

@end



@implementation TJRoundingCornerButton {
    CAShapeLayer *_shapeLayer;
}


- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        _shapeLayer = [CAShapeLayer layer];
    }
    return self;
}

- (void)setRadius:(CGFloat)radius {
    _radius = radius;
    [self setNeedsLayout];
}

- (void)setCorners:(UIRectCorner)corners {
    _corners = corners;
    [self setNeedsLayout];
}

+ (instancetype)roundingCorners:(UIRectCorner)corners radius:(CGFloat)radius {
    TJRoundingCornerButton *view = [[self alloc] initWithFrame:CGRectZero];
    view -> _radius = radius;
    view -> _corners = corners;
    return view;
    
}

- (void)layoutSubviews {
    [super layoutSubviews];

    _shapeLayer.path = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners: _corners cornerRadii:CGSizeMake(_radius, _radius)].CGPath;
    self.layer.mask = _shapeLayer;
    
}

@end
